package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cn.yunhe.dao.ReplyDao;
import cn.yunhe.dao.impl.ReplyDaoImpl;
import cn.yunhe.entity.Reply;
import cn.yunhe.entity.Topic;
import cn.yunhe.entity.User;
import cn.yunhe.util.DateUtil;

/**
 * Servlet implementation class ChildernReply
 */
@WebServlet("/ChildernReply")
public class ChildernReply extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChildernReply() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String content=request.getParameter("content");
			String ridStr=request.getParameter("rid");
			String tidStr=request.getParameter("tid");
			System.out.println("!!!!!!!!@@@@@@@@@@@@@"+ridStr);
			int rid=-1;		
			if(ridStr!=null && !ridStr.equals("")){
				rid=Integer.parseInt(ridStr);
			}
			if(content==null){
				System.out.println("评论内容不能为空");
			}
			ReplyDao replyDao=new ReplyDaoImpl();
			int tid=-1;
			if( tidStr!=null){
				 tid=Integer.parseInt(tidStr);
			}
			
			User user=(User)request.getSession().getAttribute("CURR_USER");
			System.out.println(user+"111111111111111111111");
			Reply reply = new  Reply(content, DateUtil.dateTimeToString(new Date()), user, new Topic(tid),rid);
			System.out.println(reply.toString());
			boolean result=replyDao.addReply(reply);
			PrintWriter out= response.getWriter();
			Gson g=new Gson();
			String JsonStr=g.toJson(reply);
			out.write(JsonStr);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
